package com.example.qing.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.qing.config.AuthAccess;
import com.example.qing.config.LogType;
import com.example.qing.config.MyLogs;
import com.example.qing.pojo.Comment;
import com.example.qing.pojo.Message;
import com.example.qing.pojo.Novel;
import com.example.qing.pojo.Result;
import com.example.qing.service.MessageService;
import com.example.qing.service.NovelService;
import com.example.qing.utils.ProcessComments;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    MessageService messageService;
    @Resource
    ProcessComments processComments;

    @AuthAccess
    @GetMapping()
    public Result list() {
        QueryWrapper<Message>queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        List<Message>messageList = messageService.list(queryWrapper);
        List<Message>result= processComments.processMessages(messageList);
        return Result.success(result);
    }


    @MyLogs(operation = "评论",type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        messageService.removeById(id);
        return Result.success();
    }

    @AuthAccess
    @GetMapping("/total")
    public Result total() {
        long total=messageService.count();
        return Result.success(total);
    }


    @MyLogs(operation = "评论",type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<Integer> ids){
        log.info("批量删除");
        messageService.removeByIds(ids);
        return Result.success();
    }

    @AuthAccess
    @MyLogs(operation = "评论",type = LogType.ADD)
    @PostMapping("/add")
    public Result save(@RequestBody Message message){
        message.setTime(DateUtil.now());
        messageService.save(message);
        return Result.success();
    }

    @GetMapping("/selectByPage")
    public Result adminList(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize){
        log.info("分页查询");
        Page<Message> page=messageService.page(new Page<>(pageNum,pageSize));
        return Result.success(page);
    }






}
